package loan.calc.service.calculate.impl;

import loan.calc.calculator.Calculator;
import loan.calc.dto.LoanDTO;
import loan.calc.service.calculate.CalculatorService;
import loan.calc.util.EnhanceStopWatch;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.support.ApplicationObjectSupport;
import org.springframework.stereotype.Service;

/**
 * @author v_ECD963
 */
@Service
@Slf4j
public class CalculatorServiceImpl extends ApplicationObjectSupport implements CalculatorService {

    private final Calculator[] calculators;

    public CalculatorServiceImpl(Calculator[] calculators) {
        this.calculators = calculators;
    }

    @Override
    public void calculate(LoanDTO loan) {
        EnhanceStopWatch stopWatch = new EnhanceStopWatch("CalculatorEngine");
        for (Calculator calculator : this.calculators) {
            stopWatch.start(calculator.calculatorName());
            log.debug("计算器[{}]准备执行,排序号[{}],执行前数据[{}]", calculator.calculatorName(), calculator.calculatorSort(), loan);
            calculator.preCalculate(loan);
            calculator.calculate(loan);
            calculator.postCalculate(loan);
            log.debug("计算器[{}]执行结束,执行后数据[{}]", calculator.calculatorName(), loan);
            stopWatch.stop();
        }
        log.debug(stopWatch.prettyPrintWithMillis());
    }

}
